package com.deli.wuziqi;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Auther: xudeli
 * @Date: 2018/12/4/004 40
 * @Description:
 */

public class MClient {
    /**
     * 棋盘数组
     */
    String[][] strings = new String[17][17];

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.1.2", 7878);
        new MClient().inout(socket);
    }

    /**
     * 打印棋盘
     */
    public void printChessboard() {//打印棋盘
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 17; j++) {
                if (i == 0) {
                    System.out.printf("| %02d ", j);
                } else if (j == 0) {
                    System.out.printf("| %02d ", i);
                } else {
                    if (strings[i][j] == null) {
                        strings[i][j] = "__";
                    }
                    System.out.print("| " + strings[i][j] + " ");
                }
            }
            System.out.println("|");
        }
    }

    /**
     * 判断游戏结束
     *
     * @return boolean
     */
    public boolean jugeO() {
        for (int i = 5; i < 17; i++) {
            for (int j = 5; j < 17; j++) {
                if ((strings[i][j] == "O " && strings[i - 1][j - 1] == "O "
                        && strings[i - 2][j - 2] == "O " && strings[i - 3][j - 3] == "O "
                        && strings[i - 4][j - 4] == "O ")) {
                    System.out.println("O玩家胜利");
                    return true;
                }
            }
        }
        for (int i = 1; i < 14; i++) {
            for (int j = 1; j < 14; j++) {
                if ((strings[i][j] == "O " && strings[i + 1][j + 1] == "O "
                        && strings[i + 2][j + 2] == "O " && strings[i + 3][j + 3] == "O "
                        && strings[i + 4][j + 4] == "O ")) {
                    System.out.println("O玩家胜利");
                    return true;
                }
            }
        }
        for (int i = 1; i < 17; i++) {
            for (int j = 5; j < 17; j++) {
                if ((strings[i][j] == "O " && strings[i][j - 1] == "O "
                        && strings[i][j - 2] == "O " && strings[i][j - 3] == "O "
                        && strings[i][j - 4] == "O ")) {
                    System.out.println("O玩家胜利");
                    return true;
                }
            }
        }
        for (int i = 5; i < 17; i++) {
            for (int j = 1; j < 17; j++) {
                if ((strings[i][j] == "O " && strings[i - 1][j] == "O "
                        && strings[i - 2][j] == "O " && strings[i - 3][j] == "O "
                        && strings[i - 4][j] == "O ")) {
                    System.out.println("O玩家胜利");
                    return true;
                }
            }
        }
        return false;
    }

    public boolean jugeX() {
        for (int i = 5; i < 17; i++) {
            for (int j = 5; j < 17; j++) {
                if ((strings[i][j] == "X " && strings[i - 1][j - 1] == "X "
                        && strings[i - 2][j - 2] == "X " && strings[i - 3][j - 3] == "X "
                        && strings[i - 4][j - 4] == "X ")) {
                    System.out.println("X玩家胜利");
                    return true;
                }
            }
        }
        for (int i = 1; i < 14; i++) {
            for (int j = 1; j < 14; j++) {
                if ((strings[i][j] == "X " && strings[i + 1][j + 1] == "X "
                        && strings[i + 2][j + 2] == "X " && strings[i + 3][j + 3] == "X "
                        && strings[i + 4][j + 4] == "X ")) {
                    System.out.println("X玩家胜利");
                    return true;
                }
            }
        }
        for (int i = 1; i < 17; i++) {
            for (int j = 5; j < 17; j++) {
                if ((strings[i][j] == "X " && strings[i][j - 1] == "X "
                        && strings[i][j - 2] == "X " && strings[i][j - 3] == "X "
                        && strings[i][j - 4] == "X ")) {
                    System.out.println("X玩家胜利");
                    return true;
                }
            }
        }
        for (int i = 5; i < 17; i++) {
            for (int j = 1; j < 17; j++) {
                if ((strings[i][j] == "X " && strings[i - 1][j] == "X "
                        && strings[i - 2][j] == "X " && strings[i - 3][j] == "X "
                        && strings[i - 4][j] == "X ")) {
                    System.out.println("X玩家胜利");
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 流的建立 输入输出
     *
     * @throws IOException
     */
    public void inout(Socket socket) throws IOException {
        InputStream r = socket.getInputStream();
        OutputStream w = socket.getOutputStream();
        Scanner sc = new Scanner(System.in);
        try {
            while (true) {
                while (true) {
                    printChessboard();
                    System.out.println();
                    System.out.println("**********************************************************************");
                    System.out.println("请落子 \n输入坐标(行 列 )");
                    String s2 = sc.nextLine();
                    String[] split2 = s2.split(" ");
                    if (Integer.parseInt(split2[0]) < 1 || Integer.parseInt(split2[0].trim()) > 17 ||
                            Integer.parseInt(split2[1]) < 1 || Integer.parseInt(split2[1].trim()) > 17 ||
                            strings[Integer.parseInt(split2[0])][Integer.parseInt(split2[1].trim())] != "__") {
                        System.out.println("输入错误 请重新输入");
                        continue;
                    }
                    strings[Integer.parseInt(split2[0])][Integer.parseInt(split2[1].trim())] = "O ";
                    w.write(s2.getBytes());
                    System.out.println();
                    System.out.println("**********************************************************************");
                    printChessboard();
                    if (jugeO() == true) return;
                    break;
                }

                System.out.println("请等待对方落子");
                byte[] bytes = new byte[1024];
                r.read(bytes);
                String s1 = new String(bytes);
                String[] split1 = s1.split(" ");
                strings[Integer.parseInt(split1[0])][Integer.parseInt(split1[1].trim())] = "X ";
                System.out.println();
                System.out.println("**********************************************************************");
                printChessboard();
                if (jugeX() == true) return;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
